<?php

/**
 * Class to store and handle golf round data like players, holes, etc
 */
class Round {
  private $courseName = '';
  private $numberOfHoles;
  private $player1Name;
  private $player2Name;
  private $player3Name;
  private $player4Name;
  private $player1Total;
  private $player2Total;
  private $player3Total;
  private $player4Total;
  
  public function __construct($course, $holes, $player1, $player2, $player3, $player4) {
    $this->courseName = $course;
    $this->numberOfHoles = $holes;
    $this->player1Name = $player1;
    $this->player2Name = $player2;
    $this->player3Name = $player3;
    $this->player4Name = $player4;
  }

private function pop7FromArray($args) {
  for ($i = 1; $i < 8; $i++) {
    $results = array_pop($args);
    }
  return $results;  
}

/**
 * Returns player names in Array based on POST
 * @return Array
 */
  public function getPlayerList() {
    if ($this->player1Name != null) {
      $players['1'] = $this->player1Name;
    }
    if ($this->player2Name != null) {
      $players['2'] = $this->player2Name;
    }
    if ($this->player3Name != null) {
      $players['3'] = $this->player3Name;
    }
    if ($this->player4Name != null) {
      $players['4'] = $this->player4Name;
    }    
    return $players;
  }
 
/**
 * Simple getter for total holes
 * @return {Integer}
 */
 public function getTotalHoles() {
   return $this->numberOfHoles;
 }
 
/**
 * Method to get number of players participating
 * @return {Integer}
 */
 public function getNumberOfPlayers() {
   if ($this->player1Name != null) {
      $number = 1; 
    }
    if ($this->player2Name != null) {
      $number = 2;
    }
    if ($this->player3Name != null) {
      $number = 3;
    }
    if ($this->player4Name != null) {
      $number = 4;
    }
    return $number;
 }

/**
 * Takes POST as argument and returns two dimensional results array by hole
 * @param {Array} $args Popped array
 * @return $results 
 */
public function chunkResults($args) {
// Remove everything else that results
  for ($i = 1; $i < 8; $i++) {
    $results = array_pop($args);
    }
  if ($this->player1Name != null) {
      $number = 1; 
    }
    if ($this->player2Name != null) {
      $number = 2;
    }
    if ($this->player3Name != null) {
      $number = 3;
    }
    if ($this->player4Name != null) {
      $number = 4;
    }
    $results = array_chunk($args, $number);
  return $results;
}

/**
 * Method to count total scores
 * @args {Array} $chunkedResults, {Integer} numberOfPlayers
 */
public function countTotalScores($chunkedResults, $numberOfPlayers) {
  foreach ($chunkedResults as $results) {
      $tmp = 1;
    foreach ($results as $score) {
      if ($tmp == 1) {
        $this->player1Total = $this->player1Total + $score;
      }
      if ($tmp == 2) {
        $this->player2Total = $this->player2Total + $score;
      }
      if ($tmp == 3) {
        $this->player3Total = $this->player3Total + $score;
      }
      if ($tmp == 4) {
        $this->player4Total = $this->player4Total + $score;
      }
      $tmp = ++$tmp;
    }
  }
}

/**
 * Returns player totals in Array
 * @return Array 
 */
public function getTotals() {
  $outcome;
  if ($this->player1Total != 0) {
    $outcome[] = $this->player1Total;
  }
  if ($this->player2Total != 0) {
    $outcome[] = $this->player2Total;
  }
  if ($this->player3Total != 0) {
    $outcome[] = $this->player3Total;
  }
  if ($this->player4Total != 0) {
    $outcome[] = $this->player4Total;
  }
  return $outcome; 
}

/**
 * 
 */
public function getPlayerResults($playerNumber, $chunkedArray) {
  $tmp = array();
  $pNumber = $playerNumber - 1;
  foreach ($chunkedArray as $chunk) {
    $tmp[] = $chunk[$pNumber];  
  }
//  $tmp2 = array_pop($tmp);
  
  while(count($tmp) < 18) {
    $tmp[] = 0;
  }
  
  return $tmp;
}
 
}
?>
